package com.org.khtn.command.select;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.org.khtn.ultilities.Ultilities;

public class Join extends Command{
	final String className = "Join:";

	public Join() {
		resultData = new HashMap<String, Multimap<String,String>>();
		data = new HashMap<String, Map<String,Map<String,String>>>();
		tableDefine  = new HashMap<String, Map<String,String>>();
	}


	public Map<String,Multimap<String,String>> filter(String fieldNameA, String fieldNameB)
	{
		final String methodName ="filter():";
		System.out.println(className+methodName+" vao rui:");

		String[] nameA = fieldNameA.split("\\.");
		String[] nameB = fieldNameB.split("\\.");
		int lengthA = nameA.length;
		int lengthB = nameB.length;
		int iLeft ;
		int iRight;
		String fieldA ="";
		String fieldB ="";
		System.out.println(className+methodName+" 1:");

		if (lengthA ==1 )
		{
			//kiem tra ambiguous
			if (ambiguousField(fieldNameA))
				return null;
			fieldA = fieldNameA;
		}
		System.out.println(className+methodName+" 2:");

		if (lengthB == 1)
		{
			//kiem tra ambiguous
			if (ambiguousField(fieldNameB))
				return null;
			if (fieldNameB.equalsIgnoreCase(fieldNameA))
			{
				return null;
			}
			fieldB = fieldNameB;
			
		}
		System.out.println(className+methodName+" 3:");

		if (lengthA == 2)
		{
			System.out.println(className+methodName+" nameA[0]: "+nameA[0]);
			System.out.println(className+methodName+" nameA[1]: "+nameA[1]);
			System.out.println(className+methodName+" result: "+ resultData);

			if (!tableExist(nameA[0]))
				return null;
			System.out.println(className+methodName+" 3.1:");
	
			if (!Ultilities.fieldExist(nameA[1],resultData))
				return null;
			System.out.println(className+methodName+" 3.2:");

			fieldA = nameA[1];
			
		}
		System.out.println(className+methodName+" 3:");
		System.out.println(className+methodName+" 3.1:");

		if (lengthB == 2)
		{
			System.out.println(className+methodName+" nameB[0]: "+nameB[0]);
			System.out.println(className+methodName+" nameB[1]: "+nameB[1]);

			if (!tableExist(nameB[0]))
				return null;
			if (!Ultilities.fieldExist(nameB[1],resultData))
				return null;
			fieldB = nameB[1];
			
		}
		System.out.println(className+methodName+" 4:");

		Map<String,Multimap<String,String>> result = new HashMap<String, Multimap<String,String>>();
		System.out.println(className+methodName+"resultData: "+resultData);

		for (Map.Entry<String, Multimap<String,String>> d : resultData.entrySet())
		{
			Multimap<String,String> tam = ArrayListMultimap.create();
			tam = d.getValue();
			System.out.println(className+methodName+"tam: "+tam);

			if (fieldA.equalsIgnoreCase(fieldB))
			{
				List<String> list =  (List<String>) tam.get(fieldA);
				if (list.size()==2)
				{
					if (list.get(0).equals(list.get(1)))
						result.put(d.getKey(), d.getValue());
				}
			}
			else
			{
				List a = (List) tam.get(fieldA);
				List b = (List) tam.get(fieldB);
				if (a.get(0).equals(b.get(0)))
				{
					result.put(d.getKey(), d.getValue());
					
				}
			}
		}
		System.out.println(className+methodName+" result: "+result);

		return result;
		
	}
	public Where join(String tableName,String fieldName)
	{
		final String methodName ="join():";
		String[] field =null;
		if (fieldName != null)
			field = fieldName.split("=");
		Where where = new Where();
		where.putData(resultData);
		where.setTables(tables);
		where.setData(data);
		where.databaseName =databaseName;
		where.putAllCurrentTable(currentTable);
		where.setTableDefine(tableDefine);
		System.out.println(className+methodName+" 1:");
		System.out.println(className+methodName+" tableDefine:"+tableDefine);
		
		if (tableName == null || field.length != 2)
		{
		
			return where;
		}
		if (resultData.containsKey("error"))
			return where;
		String fieldNameA = field[0];
		String fieldNameB = field[1];
		Map<String,Multimap<String,String>> table = Ultilities.mapToMultimap(getTable(tableName));
		Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();
	
		if(table==null)
		{
			resultData=Ultilities.createError("table not exist");
			where.putData(resultData);
			return where;
		}
		putCurrentTables(tableName);
		System.out.println(className+methodName+" 2:");

		result =Ultilities.mul(resultData, table);
		putData(result);
		System.out.println(className+methodName+"result 1:"+result);
		result = filter(fieldNameA, fieldNameB);
		System.out.println(className+methodName+"result 2:"+result);
		if (result !=null)
			currentTable.add(tableName);
	
		where.putData(result);
		where.setTables(tables);
		where.setData(data);
		where.putAllCurrentTable(currentTable);
		where.setTableDefine(tableDefine);

		return where;
	}

}
